<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a>首页管理</a>
        <a><cite>我们的服务</cite></a>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-fluid">
            <div class="layui-card">
                <div class="layui-card-body">
                    <table class="layui-hide" id="table-service" lay-filter="table-service"></table>
                    <script type="text/html" id="table-useradmin-webuser">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['carousel', 'form', 'admin', 'element', 'useradmin', 'table', 'laydate', 'layedit'], function() {
        var carousel = layui.carousel,
            $ = layui.$,
            setter = layui.setter,
            view = layui.view,
            table = layui.table,
            form = layui.form,
            elem = layui.element,
            admin = layui.admin,
            laydate = layui.laydate,
            layedit = layui.layedit;
        table.render({
            elem: "#table-service", //表id
            url: setter.request.baseUrl + "/service/frontend/list",
            method: "get",
            headers: {
                token: layui.data("layuiAdmin").token
            },
            response: {
                statusName: "code",
                statusCode: 0,
                msgName: "msg",
                countName: "count",
                dataName: "list",
            },
            cols: [
                [{
                    align: 'center',
                    field: "serviceId",
                    width: "3%"
                }, {
                    align: 'center',
                    field: "serviceName",
                    width: "10%",
                    title: "服务名称"
                }, {
                    align: 'center',
                    field: "serviceNameEn",
                    width: "21%",
                    title: "服务英文名称"
                }, {
                    align: 'center',
                    field: "serviceIcon",
                    width: "38%",
                    title: "服务图标"
                }, {
                    field: "time",
                    width: "7%",
                    align: 'center',
                    title: "时间",
                    templet: function(d) {
                        var star_releaseTime = layui.util.toDateString(new Date(d.time * 1000).getTime(), "yyyy/MM/dd");
                        return '<div>' + star_releaseTime + '</div>'
                    }
                }, {
                    field: "detail",
                    width: "17%",
                    align: 'center',
                    title: "详情"
                }, {
                    fixed: "right",
                    title: "操作",
                    align: 'center',
                    toolbar: "#table-useradmin-webuser"
                }, ],
            ],
            page: false,
        });

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //监听右侧的编辑模块
        table.on("tool(table-service)", function(obj) {
                var data = obj.data;
                console.log(data)
                if (obj.event === "edit") {
                    admin.popup({
                        title: "更新服务信息",
                        area: ["800px", "800px"],
                        success: function(layero, index) {
                            view(this.id)
                                .render("countpoint/servicetable", data)
                                .done(function() {
                                    let _data = data;
                                    form.render(null, "layuiadmin-form-admin");
                                    laydate.render({
                                        elem: '#date'
                                    });
                                    var time = layui.util.toDateString(new Date(_data.time * 1000).getTime(), "yyyy-MM-dd");
                                    laydate.render({
                                        elem: '#date',
                                        value: time,
                                        isInitValue: true
                                    });
                                    //富文本 
                                    layedit.set({
                                        uploadImage: {
                                            url: setter.request.baseUrl + '/upload/admin/file', //接口url
                                            type: 'post' //默认post
                                        }
                                    });
                                    var aa = layedit.build('demo');
                                    console.log(data)
                                    layedit.setContent(aa, data.detail, false)


                                    //监听提交
                                    form.on('submit(LAY-user-front-submit)', function(data) {
                                        var field = data.field; //获取提交的字段
                                        var aahtml = layedit.getContent(aa);
                                        field.detail = aahtml
                                        field.isHot = data.field.isHot == 'on' ? '1' : 0
                                        console.log(data.field)


                                        var time = field.time;

                                        time = time.substring(0, 10)

                                        field.time = new Date(time).getTime() / 1000;

                                        editUserHandle(Object.assign(_data, field));


                                        //提交 Ajax 成功后，关闭当前弹层并重载表格
                                        editUserHandle(field);
                                        layui.table.reload("table-instance");
                                        layer.close(index); //执行关闭 
                                    });
                                });
                        },
                    });
                }

            })
            //编辑
        function editUserHandle(params) {
            admin.req({
                url: setter.request.baseUrl + "/service/admin/save",
                data: JSON.stringify(params),
                type: "post",
                dataType: "json",
                contentType: "application/json",
                done: function(res) {
                    layui.table.reload("table-service");
                },
            });
        }
        layui.$('.layui-btn.layuiadmin-btn-admin').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    })
</script>